<template>
	<view class="confirmOrder">
		<view class="top" v-if="isAddress">
			<view v-if="address.id > 0" class="address" @click="jump('address', optionsParam)">
				<image :src="obtainImgUrl('icon/icon33.png')" mode="aspectFill"></image>
				<view class="right">
					<view class="txt">
						<text>收货人：{{address.realname}}</text>
						<text>{{address.mobile}}</text>
					</view>
					<view class="txt1">
						收货地址：{{address.address_name}}{{address.address_details}}
					</view>
					<view class="jt"></view>
				</view>
			</view>
			<view v-else class="noaddress" @click="jump('addOreditAddress', 'id=0&formType=1&' +optionsParam)">
				<image :src="obtainImgUrl('icon/noaddress.png')" mode="widthFix"></image>
				<text>+新建地址</text>
			</view>
			<image class="img" :src="obtainImgUrl('icon/icon34.png')" mode="aspectFill"></image>
		</view>
		<view class="main" v-for="(storeItem,index) in products" :key="index">
			<view class="top">
				<view class="left" @click="jump('store')">
					<image :src="storeItem.merchant_icon" mode="aspectFill"></image>
					<text>{{storeItem.merchant_name}}</text>
				</view>
			</view>
			<view class="goods" v-for="(item,index) in storeItem.productList" :key="index">
				<image :src="item.product_picture" mode="aspectFill"></image>
				<view class="right">
					<text>{{item.product_name}}</text>
					<view v-if="item.sku_delivery_date != ''" style="color: rgba(200, 0, 0, 1);">{{item.sku_delivery_date}}</view>
					<view class="size">
						<text>{{item.sku_name}}</text>
						<text>x{{item.number}}</text>
					</view>
					<text class="num">{{'￥'+item.price}}</text>
				</view>
			</view>
		</view>
		<view class="line">
			<text>商品金额</text>
			<text>{{productMoney}}</text>
		</view>
		<view class="line" style="position: relative;" v-if="isAddress">
			<text>快递方式</text>
			<view class="right">
				<picker v-if="formData.address_id > 0 && company.length > 0" @change="bindPickerChange" :value="selCompany" :range="company" range-key="name">
					<view class="item">{{company[selCompany].name}}</view>
				</picker>
				<view class="item" v-if="formData.address_id > 0 && company.length == 0">包邮</view>
				<view class="item red" v-if="formData.address_id == 0">请选择收货地址</view>
				<view class="item red" v-if="formData.address_id == -1">当前地址不支持配送</view>
				<view class="jt"></view>
			</view>
		</view>
		<view class="line"  v-if="isAddress">
			<text>订单邮费</text>
			<text>¥{{companyFee}}</text>
		</view>
		<view class="pic">
			<text>合计：</text>
			<text>{{'￥'+totalMoney}}</text>
		</view>
		<view class="common" v-if="isAddress">
			<text>买家留言</text>
			<textarea maxlength="500" placeholder-style="color:rgba(66,81,114,0.2);" value="formData.buyer_message"
				placeholder="留言备注..." v-model="formData.buyer_message" />
		</view>
		<view class="bottom" style="z-index: 10;">
			<text>共{{productNum}}件</text>
			<view class="pic1">
				<text>合计</text>
				<text>{{'￥'+totalMoney}}</text>
			</view>
			<view class="btn" @click="createOrder()">提交订单</view>
		</view>
		<uni-popup ref="popup" type="bottom">
			<view class="pay_type">
				<image @click="close" class="close" :src="obtainImgUrl('icon/close.png')"
					mode="aspectFill"></image>
				<view class="title">选择支付方式</view>
				<view class="paypir">
					<text>应付金额</text>
					<text>{{'￥'+payFormData.payMoney}}</text>
				</view>
				<view class="line2" v-if="account.user_money > 0" @click="selUseMoney()">
					<text>余额支付 - 当前余额：{{account.user_money}}</text>
					<view :class="formData.isUseMoney ?'sel':'nosel'"></view>
				</view>
				<view class="line2" v-if="payFormData.payMoney > 0">
					<text>在线微信支付</text>
					<image :src="obtainImgUrl('icon/icon37.png')" mode="aspectFill"></image>
				</view>
				<view class="pay_bot" @click="subPay()">确认支付</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import serve from '../../static/request.js';
	import common from '../../static/jump.js';
	export default {
		data() {
			return {
				account: {},
				isAddress: true,
				address: {},
				company: [],
				products: [],
				productNum: 0,
				productMoney: 0,
				companyFee: 0,
				totalMoney: 0,
				selCompany: 0,
				formData: {
					product_id: 0,
					type: '',
					data: '',
					address_id: 0,
					company_id: '',
					coupon_id: '',
					is_use_point: 0,
					isUseMoney: 0,
					buyer_message: '',
				},
				optionsParam: '',
				check_sub: false,
				payFormData: {
					type: '',
					payMoney: 0,
					isUseMoney: 0,
					id: 0
				},
			}
		},
		methods: {
			resetAddress(str) {
				this.formData.address_id = str.id;
				this.previewOrder();
			},
			numFilter(value) {
				let tempVal = parseFloat(value).toFixed(3);
				let realVal = tempVal.substring(0, tempVal.length - 1);
				return realVal;
			},
			previewOrder() {
				serve.request({
					url: '/store/order/preview',
					data: this.formData
				}).then(res => {
					if (res.code != 200) {
						uni.showModal({
							title: '提示',
							content: res.message,
							showCancel: false,
							success: function() {
								common.jump('return_one');
							}
						});
						return;
					}
					this.account = res.data.account;
					this.address = res.data.address;
					this.isAddress = res.data.isAddress;
					this.company = res.data.company;
					this.products = res.data.products;
					this.productNum = res.data.productNum;
					this.productMoney = res.data.productMoney;
					this.totalMoney = res.data.totalMoney;
					this.companyFee = res.data.companyFee;
					this.formData.company_id = res.data.defaultCompanyId;
					this.formData.address_id = res.data.defaultAddressId;
					this.$forceUpdate();
				});
			},
			bindPickerChange: function(e) {
				let companyItem = this.company[e.detail.value];
				this.formData.company_id = companyItem.id;
				this.companyFee = companyItem.fee;
				this.totalMoney = this.numFilter(Number(this.productMoney) + Number(this.companyFee));
				this.$forceUpdate();
			},
			close() {
				this.$refs.popup.close();
			},
			createOrder() {
				if (this.formData.address_id == 0) {
					uni.showToast({
						title: "请选择收货地址",
						icon: 'none'
					})
					return;
				}
				if (this.formData.address_id == -1) {
					uni.showToast({
						title: "当前收货地址不支持配送，请重新选择地址",
						icon: 'none'
					})
					return;
				}
				this.payFormData.payMoney = JSON.parse(JSON.stringify(this.totalMoney));
				this.$refs.popup.open();
				return;
			},
			selUseMoney() {
				this.payFormData.payMoney = JSON.parse(JSON.stringify(this.totalMoney));
				this.formData.isUseMoney = this.formData.isUseMoney == 1 ? 0 : 1;
				this.payFormData.isUseMoney = this.formData.isUseMoney;
				if(this.formData.isUseMoney) {
					this.payFormData.payMoney = this.numFilter(Number(this.payFormData.payMoney) - Number(this.account.user_money));
				}
				this.payFormData.payMoney = this.payFormData.payMoney > 0 ? this.payFormData.payMoney : 0;
			},
			addTempSetting(sceneId, tempId, status) {
				if (status == undefined) {
					return;
				}
				serve.request({
					url: '/member/main/add-temp-setting',
					data: {
						tempType: 'wechat',
						sceneId: sceneId,
						tempId: tempId,
						status: status
					}
				}).then(res => {
					console.info(res);
				});
			},
			subPay() {
				let _self = this;
				if (_self.check_sub == true) {
					return;
				}
				uni.showLoading({
					title: '提交中···',
					mask: true
				});
				_self.check_sub = true;
				serve.request({
					url: '/store/order/sub-preview',
					data: _self.formData
				}).then(res => {
					uni.hideLoading();
					_self.check_sub = false;
					if(res.code != 200) {
						uni.showModal({
							title: '提示',
							content: res.message,
							showCancel: false,
						});
						return;
					} 
					_self.payFormData.type = res.data.type;
					_self.payFormData.id = res.data.id;
					if(res.data.isPay) {
						_self.wechatPay();
						return ;
					}
					common.jump('success', 'type='+_self.payFormData.type+'&id='+_self.payFormData.id);
					return ;
				});
			},
			close() {
				this.$refs.popup.close();
			},
			wechatPay() {
				var self = this;
				serve.request({
                    url: '/store/pay/wechat',
                    data: self.payFormData
				}).then(res => {
					if (res.code != 200) {
						self.check_sub = false;
						uni.showToast({
							title: res.message,
							icon: 'none'
						});
						return;
					}
					let payConfig = res.data;
					uni.requestPayment({
						provider: 'wxpay',
						timeStamp: payConfig.timeStamp,
						nonceStr: payConfig.nonceStr,
						package: payConfig.package,
						signType: payConfig.signType,
						paySign: payConfig.paySign,
						success: function(res) {
							self.queryPayResult(payConfig.payId);
							return;
						},
						fail: function(err) {
							uni.showToast({
								title: '放弃支付',
								icon: 'none'
							});
							common.jump('success', 'type=create_order&id=' + self.payFormData.id);
							return;
						}
					})
				});
				return;
			},
			queryPayResult(payId) {
				uni.showLoading({
					title: '查询中···',
					mask: true
				});
				var self = this;
				setTimeout(function() {
					serve.request({
						url: '/payservice/query-pay',
						data: {
							payId: payId,
						}
					}).then(res => {
						if (res.data.payStatus == 1) {
							self.jump('success', 'type=pay_order&id=' + res.data.dataId);
						} else {
							self.queryPayResult(payId);
						}
					});
				}, 1500);
			},
			obtainImgUrl(imgUrl) {
				return serve.imgUrl()+imgUrl;
			},
			jump(jump_type, jump_link) {
				common.jump(jump_type, jump_link);
			},
		},
		onLoad(options) {
			if (uni.getStorageSync('access-token') == '') {
				common.jump('login');
				return;
			}
			this.formData.type = options.type;
			this.formData.data = options.data;
			this.formData.address_id = options.address_id || 0;
			this.optionsParam = 'type=' + options.type + '&data=' + options.data;
			this.previewOrder();
		},
		onPullDownRefresh() {
			console.log('refresh');
			this.previewOrder();
		}
	}
</script>

<style scoped lang="scss">
	@media only screen and (device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ratio: 3) {
		.confirmOrder {
			padding-bottom: 124upx !important;

			.bottom {
				padding-bottom: 12px !important;
			}
		}
	}

	.confirmOrder {
		padding-bottom: 100upx;
		
		.red {
			color: red;
		}

		.bottom {
			z-index: 100;
			position: fixed;
			width: 100%;
			bottom: 0upx;
			border-top: 1upx rgba(235, 236, 240, 1) solid;
			align-items: center;
			display: flex;
			box-sizing: border-box;
			height: 120upx;
			background: rgba(255, 255, 255, 1);
			box-shadow: 0px 1px 0px 0upx rgba(235, 236, 240, 1);
			padding-left: 30upx;
			font-size: 24upx;
			color: rgba(60, 72, 104, 0.5);
			padding-bottom: env(safe-area-inset-bottom);    
			padding-top: 20rpx;

			.pic1 {
				font-size: 20upx;
				margin-left: 100upx;
				color: rgba(60, 72, 104, 0.5);

				text:last-child {
					font-size: 28upx;
					font-weight: bold;
					color: rgba(255, 198, 0, 1);
					margin-left: 10upx;
				}
			}

			.btn {
				position: absolute;
				height: 100%;
				width: 300upx;
				background: rgba(235, 89, 56, 1);
				line-height: 100upx;
				text-align: center;
				font-size: 28upx;
				font-weight: bold;
				color: rgba(255, 255, 255, 1);
				top: 0;
				right: 0;
			}
		}

		.top {
			position: relative;
			border-bottom: 16upx rgba(251, 251, 253, 1) solid;

			.noaddress {
				height: 190upx;
				width: 100%;
				display: flex;
				justify-content: center;
				align-items: center;

				image {
					width: 40upx;
				}

				text {
					font-size: 32upx;
					font-weight: bold;
					color: rgba(235, 89, 56, 1);
					margin-left: 10upx;
				}
			}

			.address {
				display: flex;
				position: relative;
				padding: 53upx 70upx;
				font-size: 28upx;
				font-weight: bold;
				align-items: center;

				image {
					position: absolute;
					left: 30upx;
					width: 31upx;
					height: 37upx;
					top: 120upx;
				}

				.txt {
					display: flex;
					justify-content: space-between;
					padding-bottom: 28upx;
				}

				.jt {
					position: absolute;
					right: 30upx;
					top: 105upx;
					content: '';
					border: 1upx solid rgba(57, 72, 107, 0.5);
					border-width: 0 3upx 3upx 0;
					padding: 6upx;
					transform: rotate(-45deg);
					-webkit-transform: rotate(-45deg);
				}
			}

			.img {
				position: absolute;
				bottom: 0;
				height: 10upx;
				width: 100%;
			}
		}

		.main {
			width: 100%;

			.top {
				display: flex;
				padding: 33upx 30upx;
				box-sizing: border-box;
				justify-content: space-between;
				align-items: center;
				border-bottom: 1upx rgba(235, 236, 240, 1) solid;

				.left {
					font-size: 32upx;
					display: flex;
					align-items: center;

					image {
						width: 35upx;
						height: 33upx;
						margin-right: 13upx;
					}

					text {
						position: relative;
						padding-right: 30upx;
					}

					text::after {
						position: absolute;
						right: 0upx;
						top: 17upx;
						content: '';
						border: 1upx solid rgba(57, 72, 107, 1);
						border-width: 0 3upx 3upx 0;
						padding: 6upx;
						transform: rotate(-45deg);
						-webkit-transform: rotate(-45deg);

					}
				}

				.right {
					font-size: 32upx;
					display: flex;
					align-items: center;

					image {
						width: 27upx;
						height: 33upx;
						margin-right: 20upx;
					}

					.jt {
						position: absolute;
						right: 0upx;
						top: 65upx;
						content: '';
						border: 1upx solid rgba(57, 72, 107, 0.5);
						border-width: 0 3upx 3upx 0;
						padding: 6upx;
						transform: rotate(-45deg);
						-webkit-transform: rotate(-45deg);
					}
				}
			}

			.goods {
				box-sizing: border-box;
				display: flex;
				align-items: center;
				padding: 40upx 30upx;
				border-bottom: 1upx rgba(235, 236, 240, 1) solid;

				image {
					width: 170upx;
					height: 170upx;
					margin-right: 20upx;
				}

				.right {
					position: relative;
					width: 503upx;
					display: flex;
					justify-content: center;
					flex-direction: column;
					font-size: 32upx;

					.operate {
						position: absolute;
						right: 0upx;
						bottom: 0upx;
						width: 130upx;
						height: 48upx;
						border: 1px solid rgba(57, 72, 107, 1);
						border-radius: 24upx;
						text-align: center;
						font-size: 24upx;
						line-height: 48upx;
						font-weight: bold;
						color: rgba(60, 72, 104, 0.5);
					}

					.size {
						display: flex;
						justify-content: space-between;
						padding: 12upx 0upx;
						color: rgba(60, 72, 104, 0.5);
						font-size: 24upx;
						align-items: center;
					}

					text:first-child {
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
					}

					.num {
						font-size: 24upx;
						color: rgba(255, 198, 0, 1);
					}
				}
			}

		}

		.line {
			box-sizing: border-box;
			padding: 37upx 30upx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 28upx;
			font-weight: bold;
			color: rgba(60, 72, 104, 0.8);
			border-bottom: 1upx rgba(235, 236, 240, 1) solid;

			text:last-child {
				color: rgba(255, 198, 0, 1);
				font-size: 32upx;
			}

			.jt {
				position: absolute;
				right: 20upx;
				top: 50upx;
				content: '';
				border: 1upx solid rgba(57, 72, 107, 0.5);
				border-width: 0 3upx 3upx 0;
				padding: 6upx;
				transform: rotate(-45deg);
				-webkit-transform: rotate(-45deg);
			}

		}

		.pic {
			box-sizing: border-box;
			padding: 37upx 30upx;
			display: flex;
			justify-content: flex-end;
			font-size: 20upx;
			color: rgba(60, 72, 104, 1);
			border-bottom: 1upx rgba(235, 236, 240, 1) solid;

			text:last-child {
				color: rgba(255, 198, 0, 1);
				font-size: 28upx;
			}

			;
		}

		.common {
			box-sizing: border-box;
			padding: 37upx 30upx;
			display: flex;
			flex-direction: column;
			flex-wrap: wrap;
			align-items: flex-start;
			font-weight: bold;
			font-size: 28upx;
			color: rgba(60, 72, 104, 0.8);

			textarea {
				position: relative;
				z-index: 10;
				font-size: 28upx;
				margin-top: 34upx;
				padding: 24upx 28upx;
				height: 92upx;
				background: rgba(252, 252, 253, 1);
				border-radius: 12upx;
			}
		}

		.pay_type {
			position: relative;
			z-index: 200;
			width: 100%;
			background: #FFFFFF;
			padding-bottom: 140upx;

			.close {
				position: absolute;
				width: 23upx;
				height: 23upx;
				right: 40upx;
				top: 45upx;
			}

			.title {
				width: 100%;
				text-align: center;
				height: 100upx;
				line-height: 100upx;
				font-size: 32upx;
				border-bottom: 1upx rgba(251, 251, 253, 1) solid;
			}

			.paypir {
				display: flex;
				justify-content: center;
				flex-direction: column;
				align-items: center;
				padding: 50upx 0upx 20upx;
				font-size: 26upx;

				text:last-child {
					color: rgba(255, 200, 10, 1);
					font-size: 32upx;
					font-weight: bold;
					margin-top: 10upx;
				}
			}

			.line2 {
				width: 100%;
				box-sizing: border-box;
				padding: 30upx;
				display: flex;
				justify-content: space-between;
				font-size: 28upx;
				border-bottom: 1upx rgba(251, 251, 253, 1) solid;

				image {
					width: 40upx;
					height: 40upx;
				}
			}
			
			.nosel {
			    width: 36upx;
			    height: 36upx;
			    border: 1upx solid rgba(60, 72, 104, 1);
			    border-radius: 50%;
			}
			
			.sel {
			    background-image: url(../../static/img/check.png);
			    background-repeat: no-repeat;
			    width: 40upx;
			    height: 40upx;
			    background-size: 100%;
			}
			
			.pay_bot {
				width: 100%;
				position: fixed;
				left: 0;
				bottom: 0;
				height: 100upx;
				background: rgba(255, 200, 10, 1);
				color: #fff;
				font-size: 32upx;
				line-height: 100upx;
				text-align: center;
				border-radius: 0;

				&::after {
					border: none;
				}
			}
		}
	}
</style>